package com.ciwong.epaper.modules.pad.homeworkwidget.widget.remoteviews;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.widget.RemoteViews;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.pad.homeworkwidget.common.AppWidgetConstants;
import com.ciwong.epaper.modules.pad.homeworkwidget.widget.HomeworkWidgetProvider;
import com.ciwong.epaper.modules.pad.homeworkwidget.widget.HomeworkWidgetService;
import com.ciwong.libs.utils.CWLog;


/**
 * date :2018/8/29
 * author: lzh
 * description:作业的整体view
 * version:2.10
 */
class HomeworkRemoteViews extends RemoteViews implements IHomeworkRemoteViews {

    private String TAG = "HomeworkRemoteViews ";
    private Context mContext;

    private AppWidgetManager mAppWidgetManager;

    private int[] mAppWidgetIds;

    public HomeworkRemoteViews(Context context) {
        super(context.getPackageName(), R.layout.widget_homework);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        this.mAppWidgetManager = AppWidgetManager.getInstance(mContext);
        this.mAppWidgetIds = getAppWidgetIds(mAppWidgetManager);
//        初始化绑定listview 以及整个view点击事件
        bindListViewAdapter();
        setOnLoninClickPendingIntent();
        setOnJoinClassClickPendingIntent();
        setOnPersonalClickPendingItent();
        setOnNoHomeworkImageViewClickPendingIntent();
    }

    private Class<? extends AppWidgetProvider> getAppWidgetProvider() {
        return HomeworkWidgetProvider.class;
    }

    private Intent getProviderIntent() {
        return new Intent(mContext, getAppWidgetProvider());
    }

    private int[] getAppWidgetIds(AppWidgetManager mAppWidgetManager) {
        ComponentName provider = new ComponentName(mContext, getAppWidgetProvider());
        return mAppWidgetManager.getAppWidgetIds(provider);
    }

    public void notifyAppWidgetViewDataChanged() {
        // 更新ListView
        if (mAppWidgetManager != null) {
            int[] appIds = getAppWidgetIds(mAppWidgetManager);
            mAppWidgetManager.notifyAppWidgetViewDataChanged(appIds, R.id.lv_homework);
        } else {
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
            int[] appIds = getAppWidgetIds(appWidgetManager);
            appWidgetManager.notifyAppWidgetViewDataChanged(appIds, R.id.lv_homework);
        }
    }

    public void updateAppWidget() {
        mAppWidgetManager.updateAppWidget(mAppWidgetIds, this);
    }

    /**
     * 判断是否登录
     *
     * @param islogin
     * @param isBindPhone 是否登陆
     * @param isJoined    是否加入班级
     */
    public void checkIsLogin(boolean islogin, boolean isBindPhone, boolean isJoined) {
        //view 处理
        if (isBindPhone && isBindPhone) {
            setViewVisibility(R.id.ll_widget_login, View.VISIBLE);
            setViewVisibility(R.id.ll_widget_no_login, View.GONE);
            if (isJoined) {
                setViewVisibility(R.id.ll_joined_class, View.VISIBLE);
                setViewVisibility(R.id.ll_widget_no_join_class, View.GONE);
            } else {
                setViewVisibility(R.id.ll_joined_class, View.GONE);
                setViewVisibility(R.id.ll_widget_no_join_class, View.VISIBLE);
            }
        } else {
            setViewVisibility(R.id.ll_widget_login, View.GONE);
            setViewVisibility(R.id.ll_widget_no_login, View.VISIBLE);
        }
    }

    public void setPersonalViewHeadBitmap(Bitmap bitmap) {
        setImageViewBitmap(R.id.iv_widget_icon_small, bitmap);
        updateAppWidget();
    }

    public void setPersonalViewHeadResource(int srcId) {
        setImageViewResource(R.id.iv_widget_icon_small, srcId);
        updateAppWidget();
    }

    public void setPersonalViewSchool(String schoolInfor) {
        setTextViewText(R.id.tv_widget_school_infor, schoolInfor);
        updateAppWidget();
    }

    public void setPersonalViewVip(String strVip) {
        if ("VIP".equals(strVip) || "试用".equals(strVip)) {
            setViewVisibility(R.id.tv_widget_isvip, View.VISIBLE);
            setTextViewText(R.id.tv_widget_isvip, strVip);
        } else {
            setViewVisibility(R.id.tv_widget_isvip, View.GONE);
        }
        updateAppWidget();
    }

    public void setPersonalViewBaseIfor(String strBaseInfor) {
        setTextViewText(R.id.tv_personal_infor, strBaseInfor);
        updateAppWidget();
    }

    public void loading() {
        setViewVisibility(R.id.iv_widget_loading, View.VISIBLE);
        setViewVisibility(R.id.iv_widget_refresh, View.GONE);
        updateAppWidget();
    }

    public void loadComplete() {
        setViewVisibility(R.id.iv_widget_refresh, View.VISIBLE);
        setViewVisibility(R.id.iv_widget_loading, View.GONE);
        updateAppWidget();
    }


    /**
     * 设置个人区域点击事件
     */
    private void setOnPersonalClickPendingItent() {
        Intent intent = getProviderIntent();
        intent.setAction(AppWidgetConstants.ACTION_JUMP_MEFRAGMENT);
        PendingIntent jumpToMeFramentIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
        setOnClickPendingIntent(R.id.ll_personal_infors, jumpToMeFramentIntent);
    }

    /**
     * 设置刷新作业按钮点击事件
     */
    private void setOnRefreshClickPendingIntent() {
        Intent intent = getProviderIntent();
        intent.setAction(AppWidgetConstants.ACTION_REFRESH_CLICK);
        PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
        setOnClickPendingIntent(R.id.iv_widget_refresh, refreshPendingIntent);
    }

    /**
     * 设置没有作业页面点击事件
     */
    private void setOnNoHomeworkImageViewClickPendingIntent() {
        Intent intent = getProviderIntent();
        intent.setAction(AppWidgetConstants.ACTION_REFRESH_ALL_CLICK);
        PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
        setOnClickPendingIntent(R.id.ll_widget_no_net, refreshPendingIntent);
    }

    /**
     * 设置登录按钮点击事件
     */
    private void setOnLoninClickPendingIntent() {

        Intent intent = getProviderIntent();
        intent.setAction(AppWidgetConstants.ACTION_WIDGET_LOGIN);
        PendingIntent jionClassPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
        setOnClickPendingIntent(R.id.tv_widget_login, jionClassPendingIntent);
    }

    /**
     * 设置加入班级按钮点击事件
     */
    private void setOnJoinClassClickPendingIntent() {
        Intent intent = getProviderIntent();
        intent.setAction(AppWidgetConstants.ACTION_WIDGET_JOIN_CLASS);
        PendingIntent jionClassPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
        setOnClickPendingIntent(R.id.tv_widget_join_class, jionClassPendingIntent);
    }

    /**
     * 绑定事件listview的adapter
     */
    private void bindListViewAdapter() {
        //初始化作业列表刷新的intent
        setOnRefreshClickPendingIntent();
        //绑定listview的adapter
        int listViewResId = R.id.lv_homework;
        Intent serviceIntent = new Intent(mContext, HomeworkWidgetService.class);
        serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
        setEmptyView(listViewResId, R.id.ll_widget_no_net);//指定集合view为空时显示的view
        setRemoteAdapter(listViewResId, serviceIntent);

        // 设置响应 ListView 的intent模板
        // (01) 通过 setPendingIntentTemplate设置"intent模板"
        // (02) 然后在处理该"集合控件"的RemoteViewsFactory类的getViewAt()接口中通过 setOnClickFillInIntent设置"集合控件的某一项的数据"
        Intent listItemIntent = getProviderIntent();
        listItemIntent.setAction(AppWidgetConstants.ACTION_JUMP_LISTITEM);
        listItemIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, listViewResId);
        PendingIntent pendingIntentTemplate = PendingIntent.getBroadcast(mContext, 0, listItemIntent, 0);
        // 设置intent模板
        setPendingIntentTemplate(listViewResId, pendingIntentTemplate);
    }

}
